package com.xiaoyu.commonlib.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

import static android.content.Context.LOCATION_SERVICE;

/**
 * @author jx
 * @date 创建时间 2018/11/15
 * @Description 获取经纬度
 */
@SuppressLint("MissingPermission")
public class LongitudeAndLatitude {
    static LocationListener locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                Log.e("Map", "Location changed : Lat: "
                        + location.getLatitude() + " Lng: "
                        + location.getLongitude());
            }
        }
    };
    private static Location location;
    private static Context mContext;

    /**
     * 获取经纬度
     *
     * @param context
     * @return
     */
    public static Location getLngAndLat(Context context) {
        mContext = context;
        double latitude = 0.0;
        double longitude = 0.0;
        LocationManager locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
//        boolean ok = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//
//        if (ok) {
//            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//                //从gps获取经纬度
//                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//                if (location != null) {
//                    latitude = location.getLatitude();
//                    longitude = location.getLongitude();
//
//                } else {
//                    // 当GPS信号弱没获取到位置的时候又从网络获取
//                    return getLngAndLatWithNetwork(context);
//                }
//            } else {
//                // 从网络获取经纬度
//                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
//                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//                if (location != null) {
//                    latitude = location.getLatitude();
//                    longitude = location.getLongitude();
//                }
//            }
//
//        } else {
//            ToastUtil.showToast(context, "系统检测到未开启GPS定位服务");
//            Intent intent = new Intent();
//            intent.setAction(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//            context.startActivity(intent);
//        }
        String provider = LocationManager.NETWORK_PROVIDER;
        location = locationManager.getLastKnownLocation(provider);
        Log.i("jx", "getLngAndLat: " + location.getLatitude() + "---------" + location.getLongitude());
        return location;
    }


    //从网络获取经纬度
    public static Location getLngAndLatWithNetwork(Context context) {
        double latitude = 0.0;
        double longitude = 0.0;
        LocationManager locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
        return location;
    }
}
